package boogui.member;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import boogui.db.BooguiDAO;
import boogui.db.IBooguiDAO;
import boogui.db.MemberSQL;

public class MembersuperviseDAO implements IMembersuperviseDAO {
	IBooguiDAO bdao = null;
	MemberSQL msql = null;
	Scanner Scan = new Scanner(System.in);

	public MembersuperviseDAO() {
		bdao = new BooguiDAO();
		msql = new MemberSQL();
	}

	@Override
	public List<MemberVO> idsearchMember(MemberVO m, String str) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MemberVO> result = new ArrayList<>();

		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(msql.getSearchIdSQL());
			pstmt.setString(1, "%" + str + "%");
			rs = pstmt.executeQuery();

			while (rs.next()) {
				MemberVO mvo = new MemberVO();
				mvo.setMemno(rs.getString("memno"));
				mvo.setMemid(rs.getString("memid"));
				mvo.setMemname(rs.getString("memname"));
				mvo.setMembirth(rs.getString("membirth"));
				mvo.setMemgenre(rs.getString("memgenre"));
				mvo.setMemhp(rs.getString("memhp"));
				mvo.setMememail(rs.getString("mememail"));
				result.add(mvo);

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}

		return result;
	}

	@Override
	public List<MemberVO> hpsearchMember(MemberVO m,String str) {

		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MemberVO> result = new ArrayList<>();
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(msql.getSearchHpSQL());

			pstmt.setString(1, "%" + str + "%");

			rs = pstmt.executeQuery();
			// pstmt.setString(parameterIndex, x);
			while (rs.next()) {
				MemberVO mvo = new MemberVO();
				mvo.setMemno(rs.getString("memno"));
				mvo.setMemid(rs.getString("memid"));
				mvo.setMemname(rs.getString("memname"));
				mvo.setMembirth(rs.getString("membirth"));
				mvo.setMemgenre(rs.getString("memgenre"));
				mvo.setMemhp(rs.getString("memhp"));
				mvo.setMememail(rs.getString("mememail"));
				result.add(mvo);

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}

		return result;
	}

	@Override
	public List<MemberVO> namesearchMember(MemberVO m,String str) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MemberVO> result = new ArrayList<>();
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(msql.getSearchNameSQL());

			pstmt.setString(1, "%" + str+ "%");

			rs = pstmt.executeQuery();
			// pstmt.setString(parameterIndex, x);
			while (rs.next()) {
				MemberVO mvo = new MemberVO();
				mvo.setMemno(rs.getString("memno"));
				mvo.setMemid(rs.getString("memid"));
				mvo.setMemname(rs.getString("memname"));
				mvo.setMembirth(rs.getString("membirth"));
				mvo.setMemgenre(rs.getString("memgenre"));
				mvo.setMemhp(rs.getString("memhp"));
				mvo.setMememail(rs.getString("mememail"));
				result.add(mvo);

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}

		return result;
	}
}
